const {
  get,
  post
} = require('../../request/index')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
// import Notify from 'path/to/@vant/weapp/dist/notify/notify';




Page({

  /**
   * 页面的初始数据
   */
  data: {
    formConfig: [],
    data: {},
    net: {},
    name: '新增',
    newt: "",
    canShow: true,
    type: null,
    value1: "",
    value2: "",
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
  },

  getData(formConfig, data) {

    let temp = {}
    formConfig.map((val, index, obj) => {
      if (val.hidden != 'edit') {
        temp[val.prop] = data[val.prop]
      }
    })
    console.log(data, temp);
    if (this.data.name != "评估") {
      if (this.data.type == 1) {
        console.log(this.data.type);
        temp['flowId'] = data['flowId']
      } else {
        temp['id'] = data['id']
      }

    } else {
      temp['riskId'] = data['id']
    }


    return temp

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      name: options.titile,
      type: options.type
    })
    let arr = []
    let formConfig = options.formConfig ? JSON.parse(options.formConfig) : {}
    for (let i = 0; i < formConfig.length; i++) {
      if (formConfig[i].hidden !== "all") {
        arr.push(formConfig[i])
      }
    }
    let data = options.data ? this.getData(arr, JSON.parse(options.data)) : {}

    let newData = this.getData(arr, data)



    arr.map((it, idx) => {
      for (const key in newData) {
        if (it.prop == key) {
          it.checked = newData[key]
        }
      }
    })
    console.log(arr, newData, JSON.parse(options.net));
    this.setData({
      formConfig: arr,
      data: newData,
      net: JSON.parse(options.net)
    })

  },
  changeboxbl(e) {

    this.data.formConfig.map((it, index) => {
      if (it.prop == e.target.dataset.prop) {
        it.checked = !it.checked
      }
    })
    let a = this.data.data;

    a[e.target.dataset.prop] = e.detail

    this.setData({
      data: a,
      formConfig: this.data.formConfig
    })


  },
  checkChange(e) {


    this.data.formConfig.map((it, index) => {
      if (it.prop == e.target.dataset.prop) {
        it.checked = !it.checked
      }
    })
    let a = this.data.data;

    a[e.target.dataset.prop] = Number(e.detail)

    this.setData({
      data: a,
      formConfig: this.data.formConfig
    })



  },

  changeValue1(e) {
    this.setData({
      value1: e.detail,
    })
    let a = this.data.data
    if (this.data.value2) {
      a[e.currentTarget.dataset.prop] = `${e.detail},${this.data.value2}`
      this.setData({
        data: a,
      })
    }





  },
  changeValue2(e) {
    this.setData({
      value2: e.detail,
    })
    let a = this.data.data
    if (this.data.value1) {
      a[e.currentTarget.dataset.prop] = `${this.data.value1},${e.detail}`
      this.setData({
        data: a,
      })
    }

  },
  ddchange(e) {
    let {
      net,
      prolist
    } = this.data;
    let a = this.data.data
    a[e.currentTarget.dataset.prop] = e.detail + ''
    this.setData({
      data: a,
    })
  },
  change(e) {

    console.log(this.data);
    let {
      net,
      prolist
    } = this.data;
    let a = this.data.data
    if (net.inputType == 'inputType') {
      if (e.currentTarget.dataset.code) {
        post('/app/specialWorkerInfo/getList', {
          mineId: prolist.mineId,
        }).then(res => {

          let resuls = res.data.list.find((value, index) => {
            return e.detail == value.empId
          })
          a[e.currentTarget.dataset.code] = resuls.certificateNo
          a[e.currentTarget.dataset.prop] = e.detail
          this.setData({
            data: a,
          })
        })
      } else {
        console.log(e.currentTarget.dataset.prop);
        a[e.currentTarget.dataset.prop] = e.detail
        this.setData({
          data: a,
        })
      }

    } else {
      a[e.currentTarget.dataset.prop] = e.detail

      this.setData({
        data: a,
      })

    }
  },





  changea(e) {


    let a = this.data.data


    a[e.currentTarget.dataset.prop] = e.detail.a
    a[e.currentTarget.dataset.prat] = e.detail.c




    this.setData({
      data: a,
      newt: e.detail.b
    })



  },
  canchang(e) {


    this.setData({
      canShow: false
    })


  },
  closechang(e) {

    this.setData({
      canShow: true
    })

  },

  canvasChang(e) {
    let {
      data
    } = this.data

    for (const key in data) {
      if (Object.hasOwnProperty.call(data, key)) {
        const element = data[key];
        if (e.target.dataset.prop === key) {
          data[key] = e.detail
        }

      }
    }

  },

  requiredCheck() {
    for (var i = 1; i < this.data.formConfig.length; i++) {
      if (this.data.formConfig[i].required) {

        if (this.data.data[this.data.formConfig[i].prop] == undefined) {
          return '操作失败! ' + this.data.formConfig[i].label + '是必填项.'
        }
      }
    }
    return true
  },
  next1() {
    console.log('8888');
    const {
      method,
      url,
      data
    } = this.data.net

    if (data.mineId == true) {
      let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
      data.mineId = prolist.mineId
    }

    for (let x in data) {
      if (data[x] == "userid") {
        let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
        data[x] = prolist.userid
      }
    }

    Notify({
      color: '#ad0000',
      background: '#ffe1e1',
      message: this.requiredCheck()
    });

    if (this.requiredCheck() == true) {
      if (data) {
        Object.assign(data, data, this.data.data)
      }
      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {}
      }


      wx.navigateTo({
        url: `/pages/nextstep/nextstep?url=${url}&data=${JSON.stringify(this.data.data)}`
      })


    }

  },
  next2() {
    const {
      method,
      url,
      data
    } = this.data.net
    console.log(data);

    if (data.mineId == true) {
      let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
      data.mineId = prolist.mineId
    }

    for (let x in data) {
      if (data[x] == "userid") {
        let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
        data[x] = prolist.userid
      }
    }

    Notify({
      color: '#ad0000',
      background: '#ffe1e1',
      message: this.requiredCheck()
    });

    if (this.requiredCheck() == true) {
      if (data) {
        Object.assign(data, data, this.data.data)
      }

      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {}
      }
      let data = JSON.stringify(this.data.data)
      wx.navigateTo({
        url: `/pages/nexttew/nexttew?data=${data}`
      })


    }
  },
  submitButtonTap() {

    const {
      method,
      url,
      data
    } = this.data.net

    if (data.mineId == true) {
      let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
      data.mineId = prolist.mineId
    }

    for (let x in data) {
      if (data[x] == "userid") {
        let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
        data[x] = prolist.userid
      }
    }

    Notify({
      color: '#ad0000',
      background: '#ffe1e1',
      message: this.requiredCheck()
    });

    if (this.requiredCheck() == true) {
      if (data) {
        Object.assign(data, data, this.data.data)
      }
      console.log(data);

      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {

          if (key == 'q') {
            data['h'] = `${data['h']},${data['q']}`
            delete data['q']
          }

        }
      }

      post(url, data).then(res => {

        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500,
          mask: true
        })
        wx.navigateBack({
          delta: 1
        })
      }).catch(err => {
        wx.showToast({
          title: `提交失败, 原因: ${err}`,
          icon: 'success',
          duration: 1500,
          mask: true
        })
      })
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


})